#include "include/plane.h"
// 解决byte命名冲突问题
#define byte win_byte_override
#include "include/mysql.h"
#undef byte


// 全局变量
extern MysqlUser* mysql;

// 构造和析构函数
Plane::Plane() :planeId(0), seatSize(0) {}
Plane::~Plane() {}

// CRDU
bool Plane::AddPlane(unsigned int planeId, string planeModel, unsigned int seatSize) {
    // SQL语句
    std::stringstream sqlStream;
    sqlStream << "INSERT INTO plane (planeId, planeModel, seatSize) VALUES ("
       << planeId << ", '" << planeModel << "', " << seatSize << ")";
    std::string sqlStr = sqlStream.str();
    if (!mysql->TransactionExecuteQuery(sqlStr)) {
        return false;
    }
    return true;
}
bool Plane::DeletePlane(unsigned int planeId) {
    std::stringstream sqlStream;
    sqlStream << "delete from plane where planeId = " << planeId;
    std::string sqlStr = sqlStream.str();
    if (!mysql->TransactionExecuteQuery(sqlStr)) {
        return false;
    }
    return true;
}
bool Plane::ModifyPlane(unsigned int planeId, map<string, string> entry) {


    map<string, string>::iterator it = entry.begin();
    std::stringstream sqlStream;
    sqlStream << "update plane set " << it->first << " = '"<< it->second << "' where planeId = "<< planeId;
    std::string sqlStr = sqlStream.str();
    if (!mysql->TransactionExecuteQuery(sqlStr)) {
        return false;
    }
    return true;
}

Plane Plane::SearchPlane(unsigned int planeId) {
    std::stringstream sqlStream;
    sqlStream << "select * from plane where planeId = " << planeId;
    std::string sqlStr = sqlStream.str();

    vector<vector<string>> result = mysql->select(sqlStr);
    
    this->setPlaneId(stoi(result[0][0]));
    this->setPlaneModel(result[0][1]);
    this->setSeatSize(stoi(result[0][2]));
    
    return *this;
}


// 查询所有飞机
vector<Plane> Plane::SearchAllPlane() {
    vector<Plane> planes;
    string sqlStr = "select * from plane";
    vector<vector<string>> results = mysql->select(sqlStr);

    // 对结果进行装载
    vector<vector<string>>::iterator it = results.begin();
    for (it; it != results.end();it++) {
        vector<string>::iterator it2 = (*it).begin();

        Plane p;
        p.setPlaneId(stoi(it2[0]));
        p.setPlaneModel(it2[1]);
        p.setSeatSize(stoi(it2[2]));

        planes.push_back(p);
    }
    return planes;
}




// 成员变量访问和修改函数
unsigned int Plane::getPlaneId()const {
    return planeId;
}
void Plane::setPlaneId(unsigned int id) {
    this->planeId = id;
}

string Plane::getPlaneModel()const {
    return planeModel;
}
void Plane::setPlaneModel(string model) {
    this->planeModel = model;
}

unsigned int Plane::getSeatSize()const {
    return seatSize;
}
void Plane::setSeatSize(unsigned int seatSize) {
    this->seatSize = seatSize;
}